package com.example.backend.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.dto.BorrowDTO;
import com.example.backend.entity.Book;
import com.example.backend.entity.Borrow;
import com.example.backend.entity.User;
import com.example.backend.mapper.BookMapper;
import com.example.backend.mapper.BorrowMapper;
import com.example.backend.service.BorrowService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;

@Service
public class BorrowServiceImpl implements BorrowService {
    
    private final BorrowMapper borrowMapper;
    private final BookMapper bookMapper;
    
    public BorrowServiceImpl(BorrowMapper borrowMapper, BookMapper bookMapper) {
        this.borrowMapper = borrowMapper;
        this.bookMapper = bookMapper;
    }
    
    @Override
    public Page<BorrowDTO> page(Integer current, Integer size, Long userId) {
        Page<BorrowDTO> page = new Page<>(current, size);
        return borrowMapper.getBorrowPage(page, userId);
    }
    
    @Override
    @Transactional
    public void borrow(Long bookId) {
        // 检查库存
        Book book = bookMapper.selectById(bookId);
        if (book == null || book.getStock() <= 0) {
            throw new RuntimeException("图书库存不足");
        }
        
        // 创建借阅记录
        Borrow borrow = new Borrow();
        borrow.setBookId(bookId);
        borrow.setUserId(getCurrentUserId());
        borrow.setStatus(0);
        borrowMapper.insert(borrow);
    }
    
    @Override
    @Transactional
    public void approve(Long id) {
        Borrow borrow = borrowMapper.selectById(id);
        if (borrow == null) {
            throw new RuntimeException("借阅记录不存在");
        }
        
        // 更新借阅状态
        borrow.setStatus(1);
        borrow.setBorrowTime(LocalDateTime.now());
        borrowMapper.updateById(borrow);
        
        // 减少库存
        Book book = bookMapper.selectById(borrow.getBookId());
        book.setStock(book.getStock() - 1);
        bookMapper.updateById(book);
    }
    
    @Override
    public void reject(Long id) {
        Borrow borrow = borrowMapper.selectById(id);
        if (borrow == null) {
            throw new RuntimeException("借阅记录不存在");
        }
        
        borrow.setStatus(3);
        borrowMapper.updateById(borrow);
    }
    
    @Override
    @Transactional
    public void returnBook(Long id) {
        Borrow borrow = borrowMapper.selectById(id);
        if (borrow == null) {
            throw new RuntimeException("借阅记录不存在");
        }
        
        // 更新借阅状态
        borrow.setStatus(2);
        borrow.setReturnTime(LocalDateTime.now());
        borrowMapper.updateById(borrow);
        
        // 增加库存
        Book book = bookMapper.selectById(borrow.getBookId());
        book.setStock(book.getStock() + 1);
        bookMapper.updateById(book);
    }
    
    private Long getCurrentUserId() {
        // TODO: 从当前登录用户中获取用户ID
        return 1L;
    }
} 